var mylinebar = new EchartsLineBar("fault_linebar");

$(function() {
	initQueryYear();
	initdatas();
	var t1 = window.setInterval(function() {
		var allwidth = $('#allwidth').width();
		if (allwidth != '0') {
			$("#fault_linebar").css("width", parseInt(allwidth) / 2);
			$("#fault_pie").css("width", parseInt(allwidth) / 2);
			$("#fault_barcharts").css("width", parseInt(allwidth));
			mylinebar.getChart().resize();
			window.clearInterval(t1);
		}
	}, 20);
});

function initQueryYear() {
	var d = new Date();
	var currentYear=d.getFullYear();
	$('#firstYear').val(currentYear-5);
	$('#firstYear').datetimepicker({
		startView: 4,
		minView:4,
		autoclose : true,
		format : 'yyyy'
	});
	$('#lastYear').val(currentYear);
	$('#lastYear').datetimepicker({
		startView: 4,
		minView:4,
		autoclose : true,
		format : 'yyyy'
	});
}


//按照年升序排列
function yearSortAsc(a,b) {
	return a - b;
}
//按照年升序排列
var by = function(name) {
	 return function(o, p){
	   var a, b;
	   if (typeof o === "object" && typeof p === "object" && o && p) {
	     a = o[name];
	     b = p[name];
	     if (a === b) {
	       return 0;
	     }
	     if (typeof a === typeof b) {
	       return a < b ? -1 : 1;
	     }
	     return typeof a < typeof b ? -1 : 1;
	   }
	   else {
	     throw ("error");
	   }
	}
}

function initdatas() {
	var startYear = $("#firstYear").val();
	var endYear = $("#lastYear").val();
	var indexV = document.getElementById("queryForm1:vol").selectedIndex; // 获取当前选择项的索引
	var voltage = document.getElementById("queryForm1:vol").options[indexV].value; // 获取当前选择项的值
	// 获取多年的数据
	getElectricFaultByYears(startYear,endYear,voltage);
	// 获取某一年的
	getElectricFaultByYear(startYear,voltage);
}

// 查询多年的数据
function getElectricFaultByYears(startYear,endYear,voltage) {
	$.getJSON('/rest/electricityGeneration/getElectricityFaultCountData/'+startYear+'/'+endYear+'/'+voltage,function(result) {
		result.sort(by("name"));
		getElectricityBarsLine(result);
	});
}

// 查询某一年的数据
function getElectricFaultByYear(year,voltage) {
	$.getJSON('/rest/equipmentFault/getElectricFaultByYearAndVoltage/' + year + '/' + voltage, function(result) {
		getElectricityPie(result);
		getElectricityBar(result);
	});
}

// 发电机运行情况，图一
function getElectricityBarsLine(datas) {
	var yleftname = '发电机台数';
	var yrightname = '年故障率';
	var legendLabels = [ '发电机台数', '年故障率' ];
	var ratios = [];
	var electricityNum = [];
	var faultNum = [];
	var years = [];
	for ( var i in datas) {
		electricityNum.push(datas[i].value[0].value);
		faultNum.push(datas[i].value[1].value);
		years.push(datas[i].name);
	}
	for (var i = 0; electricityNum.length > i; i++) {
		var ratio = 0;
		if(electricityNum[i]>0) {
			ratio = (parseInt(faultNum[i]) / parseInt(electricityNum[i])) * 100;
		}
		ratios.push(ratio.toFixed(2));
	}
	
	var startYear = $("#firstYear").val();
	var endYear = $("#lastYear").val();
	
	$("#run").empty();
	$("#run").prepend(startYear + "-" + endYear + "年南方电网发电机运行情况");
	if ($("#year").empty()) {
		$("#year").append(startYear + "年发电机故障类型分析");
	} else {
		$("#year").append("");
	}
	if ($("#tongji").empty()) {
		$("#tongji").append(startYear + "年南方电网及以上发电机故障类型统计")
	} else {
		$("#tongji").append("");
	}
	mylinebar.setLegendLabels(legendLabels);
	mylinebar.setNumber(electricityNum);
	mylinebar.setRatio(ratios);
	mylinebar.setYears(years);
	mylinebar.setYleftname(yleftname);
	mylinebar.setYrightname(yrightname);
	mylinebar.show();
}

//pie
function getElectricityPie(datas) {
	if(datas[0].faultType.length==0) {
		$("#fault_pie").html("该时间暂无数据信息");
	} else {
		var faultpie = new Pie("fault_pie");
		var faultTypeData = [];
		for ( var i in datas[0].faultType) {
			var num = [];
			num.push(datas[0].faultType[i]);
			num.push(0);
			faultTypeData.push(num);
		}
		for ( var i in faultTypeData) {
			for (var j = 1; datas.length > j; j++) {
				faultTypeData[i][1] += parseInt(datas[j].value[i]);
			}
		}
		var option = faultpie.getOption();
		option.legend.orient = 'vertical';
		faultpie.setLegendPosition('left', 'left');
		faultpie.setPie(faultTypeData);
		faultpie.setPieOuterRadius('50%');
		faultpie.show();
	}
}

mylinebar.getChart().on("click",function(params) {
	var year = params.name;
	var indexV = document.getElementById("queryForm1:vol").selectedIndex; // 获取当前选择项的索引
	var voltage = document.getElementById("queryForm1:vol").options[indexV].value; // 获取当前选择项的值
	getElectricFaultByYear(year,voltage);
	if ($("#year").empty()) {
		$("#year").append(year + "年发电机故障类型分析");
	} else {
		$("#year").append("");
	}
	if ($("#tongji").empty()) {
		$("#tongji").append(year + "年南方电网发电机故障类型统计")
	} else {
		$("#tongji").append("");
	}
});

function getElectricityBar(datas) {
	if(datas[0].faultType.length==0) {
		$("#fault_barcharts").html("该时间暂无数据信息");
	} else {
		var faultTypeData = {
				name : '',
				xData : {
					name : '单位',
					value : []
				},
				yData : {
					name : '次数',
					value : []
				}
		};
		for (var i = 1; datas.length > i; i++) {
			faultTypeData.xData.value.push(datas[i].name);
		}
		;
		for ( var j in datas[0].faultType) {
			var obj = {};
			obj["lineValue"] = [];
			obj["lineName"] = datas[0].faultType[j];
			for (var i = 1; datas.length > i; i++) {
				obj["lineValue"].push(datas[i].value[j]);
			}
			faultTypeData.yData.value.push(obj);
		}
		var faultTypeCompanyBar = new CategoryBar('fault_barcharts');
		var opt = faultTypeCompanyBar.getOption();
		opt.tooltip.axisPointer.type = "shadow";
		opt.legend.top = 10;
		opt.grid.top = '30%'; // 图形位置
		opt.title.textStyle.fontSize = 16;
		opt.title.textStyle.fontWeight = 'normal';
		faultTypeCompanyBar.setTitlePosition('center').setDataZoom(true).setDataZoomStartValue(0, 10);
		faultTypeCompanyBar.setData(faultTypeData);
		faultTypeCompanyBar.show();
	}
}